package com.lc.config.interceptor;

import com.lc.bean.User;
import com.lc.config.JWTUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor{
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private JWTUntil jwtUntil;

//    @Override //前置拦截
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        return true;
//    }

    @Override  //后置拦截
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
           User user =(User) redisTemplate.opsForValue().get("user");

           String usertoken=jwtUntil.token(user);

           redisTemplate.opsForValue().set("usertoken",usertoken);


    }

    @Override  //清理
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
